import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { residentApi } from '@/api/resident'
import type { ResidentHouseholdVO, ResidentHouseholdAddDTO } from '@/types/resident'

export const useResidentStore = defineStore('resident', () => {
  // 状态
  const residentList = ref<ResidentHouseholdVO[]>([])
  const total = ref(0)
  const loading = ref(false)

  // 计算属性
  const hasResidents = computed(() => residentList.value.length > 0)

  // 获取住户列表
  const getResidentList = async (params: any) => {
    loading.value = true
    try {
      const response = await residentApi.getResidentList(params)
      if (response.code === 200) {
        residentList.value = response.data.records || []
        total.value = response.data.total || 0
      } else {
        throw new Error(response.message || '获取住户列表失败')
      }
    } catch (error) {
      console.error('获取住户列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据住户ID获取住户信息
  const getResidentById = async (residentId: number) => {
    try {
      const response = await residentApi.getResidentById(residentId)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '获取住户信息失败')
      }
    } catch (error) {
      console.error('获取住户信息失败:', error)
      throw error
    }
  }

  // 新增住户基本信息
  const addResident = async (data: any) => {
    try {
      const response = await residentApi.addResident(data)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '新增住户失败')
      }
    } catch (error) {
      console.error('新增住户失败:', error)
      throw error
    }
  }

  // 更新住户基本信息
  const updateResident = async (id: number, data: any) => {
    try {
      const response = await residentApi.updateResident(id, data)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '更新住户失败')
      }
    } catch (error) {
      console.error('更新住户失败:', error)
      throw error
    }
  }

  // 删除住户基本信息
  const deleteResident = async (id: number) => {
    try {
      const response = await residentApi.deleteResident(id)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '删除住户失败')
      }
    } catch (error) {
      console.error('删除住户失败:', error)
      throw error
    }
  }

  // 添加住户到房户
  const addResidentToHousehold = async (householdId: number, data: ResidentHouseholdAddDTO) => {
    try {
      const response = await residentApi.addResidentToHousehold(householdId, data)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '添加住户失败')
      }
    } catch (error) {
      console.error('添加住户失败:', error)
      throw error
    }
  }

  // 更新住户房户关联
  const updateResidentHousehold = async (id: number, data: ResidentHouseholdAddDTO) => {
    try {
      const response = await residentApi.updateResidentHousehold(id, data)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '更新住户失败')
      }
    } catch (error) {
      console.error('更新住户失败:', error)
      throw error
    }
  }

  // 移除住户从房户
  const removeResidentFromHousehold = async (householdId: number, residentId: number) => {
    try {
      const response = await residentApi.deleteResidentFromHousehold(householdId, residentId)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '移除住户失败')
      }
    } catch (error) {
      console.error('移除住户失败:', error)
      throw error
    }
  }

  // 获取住户详情
  const getResidentDetail = async (residentId: number) => {
    try {
      const response = await residentApi.getResidentDetail(residentId)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '获取住户详情失败')
      }
    } catch (error) {
      console.error('获取住户详情失败:', error)
      throw error
    }
  }

  // 获取住户统计信息
  const getResidentStatistics = async () => {
    try {
      const response = await residentApi.getResidentStatistics()
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '获取住户统计失败')
      }
    } catch (error) {
      console.error('获取住户统计失败:', error)
      throw error
    }
  }

  // 搜索住户
  const searchResidents = async (params: any) => {
    try {
      const response = await residentApi.searchResidents(params)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '搜索住户失败')
      }
    } catch (error) {
      console.error('搜索住户失败:', error)
      throw error
    }
  }

  // 获取住户关系列表
  const getResidentRelations = async (residentId: number) => {
    try {
      const response = await residentApi.getResidentRelations(residentId)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '获取住户关系失败')
      }
    } catch (error) {
      console.error('获取住户关系失败:', error)
      throw error
    }
  }

  // 添加住户关系
  const addResidentRelation = async (data: any) => {
    try {
      const response = await residentApi.addResidentRelation(data)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '添加住户关系失败')
      }
    } catch (error) {
      console.error('添加住户关系失败:', error)
      throw error
    }
  }

  // 删除住户关系
  const deleteResidentRelation = async (relationId: number) => {
    try {
      const response = await residentApi.deleteResidentRelation(relationId)
      if (response.code === 200) {
        return response.data
      } else {
        throw new Error(response.message || '删除住户关系失败')
      }
    } catch (error) {
      console.error('删除住户关系失败:', error)
      throw error
    }
  }

  // 设置住户列表
  const setResidentList = (list: ResidentHouseholdVO[]) => {
    residentList.value = list || []
  }

  // 重置状态
  const resetState = () => {
    residentList.value = []
    total.value = 0
    loading.value = false
  }

  return {
    // 状态
    residentList,
    total,
    loading,
    
    // 计算属性
    hasResidents,
    
    // 方法
    getResidentList,
    getResidentById,
    addResident,
    updateResident,
    deleteResident,
    addResidentToHousehold,
    updateResidentHousehold,
    removeResidentFromHousehold,
    getResidentDetail,
    getResidentStatistics,
    searchResidents,
    getResidentRelations,
    addResidentRelation,
    deleteResidentRelation,
    setResidentList,
    resetState
  }
}) 